<template>
  <el-dialog
    width="800px"
    :title="$t('区域管理')"
    destroy-on-close
    append-to-body
    :visible.sync="show"
    :before-close="cancel"
  >
    <div style="margin-bottom: 10px; display: flex; justify-content: space-between">
      <el-button v-hasPermi="['distribution:stockPositionArea:add']" type="primary" size="mini" @click="handleAdd">
        {{ $t('新增') }}
      </el-button>
      <el-button icon="el-icon-refresh" circle @click="getList" size="mini"></el-button>
    </div>
    <el-table
      :height="calTableHeight() - 150"
      ref="TableRef"
      :data="tableData"
      @selection-change="(rows) => (tableCheck = rows)"
    >
      <el-table-column show-overflow-tooltip align="center" :label="$t('区域名称')" prop="areaName" />
      <el-table-column show-overflow-tooltip align="center" :label="$t('标识号')" prop="areaMarkers" />
      <el-table-column show-overflow-tooltip align="center" :label="$t('现有库位数量')" prop="nowStockNum" />
      <!-- <el-table-column show-overflow-tooltip align="center" :label="$t('备注')" prop="remark" /> -->
      <el-table-column show-overflow-tooltip align="center" :label="$t('操作')">
        <template #default="{ row }">
          <el-button v-hasPermi="['distribution:stockPositionArea:edit']" type="text" @click="handleEdit(row)">
            {{ $t('编辑') }}
          </el-button>
          <el-button v-hasPermi="['distribution:stockPositionArea:remove']" type="text" @click="handleDelete(row)">
            {{ $t('删除') }}
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <div slot="footer">
      <el-button @click="cancel">
        {{ $t('关闭') }}
      </el-button>
    </div>
    <AddAndEdit
      :show.sync="addAndEdit.show"
      :formData="addAndEdit.formData"
      :type="addAndEdit.type"
      @getList="getList"
    />
  </el-dialog>
</template>
<script>
import { deleteStockPositionArea, getStockPositionAreaList } from '@/api/distribution/sku/inventoryLocationlList'
import AddAndEdit from './addAndEdit.vue'

export default {
  props: {
    /**
     * formData
     */
    formData: {
      type: Object,
      default: () => ({}),
    },
    /**
     * 控制显示隐藏
     */
    show: {
      type: Boolean,
      required: true,
      default: false,
    },
  },
  components: { AddAndEdit },
  data() {
    return {
      addAndEdit: {
        show: false,
        type: '',
        formData: {},
      },
      /**显示搜索条件 */
      showSearch: true,
      /**表格选中项 */
      tableCheck: [],
      /**表格数据 */
      tableData: [],
      loading: false,
      /**总条数 */
      total: 0,
      /**分页相关查询参数 */
      queryParams: {
        pageNum: 1,
        pageSize: 10,
      },
    }
  },
  computed: {},
  watch: {
    show: function (newValue) {
      if (newValue) {
        this.getList()
      }
    },
  },
  methods: {
    handleAdd(row) {
      this.addAndEdit.type = 'add'
      this.addAndEdit.formData = { ...row }
      this.addAndEdit.show = true
    },
    handleEdit(row) {
      this.addAndEdit.type = 'edit'
      this.addAndEdit.formData = { ...row }
      this.addAndEdit.show = true
    },
    handleDelete(row) {
      this.$confirm(this.$t('确认删除?'), this.$t('提示'), {
        type: 'warning',
      }).then(() => {
        deleteStockPositionArea(row.id).then((res) => {
          const { code, data, msg } = res
          if (code === 200) {
            this.$message({ type: 'success', message: msg })
            this.getList()
          }
        })
      })
    },
    handleSubmit() {
      // TODO
      this.$refs['formRef'].validate((valid) => {
        if (valid) {
        }
      })
      this.cancel()
    },
    getList() {
      this.loading = true
      getStockPositionAreaList(this.queryParams)
        .then((res) => {
          this.tableData = res.rows
          this.total = res.total
        })
        .finally(() => (this.loading = false))
    },

    cancel() {
      this.$emit('update:show', false)
    },
    /** 重置按钮操作 */
    resetQuery() {
      Object.keys(this.queryParams).forEach((key) => (this.queryParams[key] = null))
      this.queryParams.pageNum = 1
      this.queryParams.pageSize = 10
      this.getList()
    },
  },
}
</script>
<style scoped lang="scss"></style>
